<?php
require_once GALAXYTOOL_ROOT."/secret/includes/error_object.inc.php";

$_SESSION['lang'] = "english"; // default language until now
@require GALAXYTOOL_ROOT."/secret/languages.php";

class GalaxypluginValidation{

	private $dbcon         = null;
	private $token         = "";
	private $usertable     = "";
	private $universe      = "";

	private $mysql_error        = null;
	public  $error_object       = null;

	function __construct($dbcon,$utablename,$token,$universe) {
		if ($dbcon == null) throw new InvalidArgumentException("dbconn must be provided");
		if ($utablename == "") throw new InvalidArgumentException("usertable must be provided");
		if ($universe == "") throw new InvalidArgumentException("universe must be provided");

		$this->dbcon         = $dbcon;
		$this->usertable     = $utablename;
		$this->token         = mysql_real_escape_string($token);
		$this->universe      = $universe;

		return $this;
	}

	public function get_insert_permissions() {

		if (strlen($this->token) != 32) {
			$this->error_object = new ErrorObject(ErrorObject::severity_error , L_TOKENFAILURE);
			return false;
		}
		// check permissions
		$query = "SELECT * FROM $this->usertable WHERE plugin_token='$this->token' AND status='active'";
		$login_error_message = new ErrorObject(ErrorObject::severity_error , L_TOKENFAILURE);
		$res = $this->run_mysql_query($query);
		if (!$res) {
			$this->error_object = new ErrorObject(ErrorObject::severity_error , "MySQL error occurred while selecting player at database");
			$this->error_object->add_child_message($this->get_last_mysql_error());
			return false;
		}

		if (mysql_num_rows($res) != 1) {
			$this->error_object = $login_error_message;
			return false;
		}

		$line = mysql_fetch_object($res);


		$version = explode(".",str_replace("v","",VERSION));
		$version_xml_tag = '<version major="'.$version[0].'"';
		if (isset($version[1])) {
			$version_xml_tag .= ' minor="'.$version[1].'"';
			if (isset($version[2])) $version_xml_tag .= ' revision="'.$version[2].'"';
		}
		$version_xml_tag .="/>";

		$result_text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
		"<galaxytool universe=\"$this->universe\">\n".
		$version_xml_tag."\n".
		"<permission name=\"caninsert\">$line->caninsert</permission>\n".
		"</galaxytool>\n";


		return $result_text;
	}


	/**
	 * Execute a query at MySQL and add errors to mysql_error object.
	 *
	 * @param String $query Query to be executed.
	 * @return boolean
	 */
	private function run_mysql_query($query) {
		$this->set_last_mysql_error(null); // clear mysql errors

		$res = mysql_query($query,$this->dbcon);
		if (!$res) {
			$error_object = new ErrorObject(ErrorObject::severity_error, mysql_error());
			$error_object->add_child_message(new ErrorObject(ErrorObject::severity_info , $query));
			$this->set_last_mysql_error($error_object);
			return false;
		} else {
			return $res;
		}
	}

	private function set_last_mysql_error(ErrorObject $errorObject = NULL) {
		$this->mysql_error = $errorObject;
	}

	private function get_last_mysql_error() {
		return $this->mysql_error;
	}

}




?>